package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {


    @Override
    public ResponseResult findListByQuery(ChannelDTO dto) {
        //校验业务参数
        //检查分页
        dto.checkParam();
        //执行分页条件查询
        Page<AdChannel> pageReq = new Page<>(dto.getPage(),dto.getSize());
        //分页条件对象
        //查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        if (dto.getStatus() != null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        queryWrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = this.page(pageReq,queryWrapper);
        //封装通用分页返回结果
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());

    }


    @Override
    public  ResponseResult insert(AdChannel adChannel) {
        //参数校验
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能未空");
        }
        if (name.length() > 10){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"名称不能大于10个字符");
        }
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,name));
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
        }
        //保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //返回封装结果
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id为空");
        }
        AdChannel oldChannel = this.getById(adChannel.getId());
        String newName = adChannel.getName();
        if (StringUtils.isNotBlank(newName) && !newName.equals(oldChannel.getName())) {
            if (newName.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能大于10个字符");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newName));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已经存在");
            }
        }
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3.删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
